package org.ldx.lotterysystem.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.ldx.lotterysystem.dao.dataobject.ActivityPrizeDo;

import java.util.List;

@Mapper
public interface ActivityPrizeMapper {
    @Insert("<script>" +
            " insert into activity_prize (activity_id, prize_id, prize_amount, prize_tiers, status)" +
            " values <foreach collection = 'items' item='item' index='index' separator=','>" +
            " (#{item.activityId}, #{item.prizeId}, #{item.prizeAmount}, #{item.prizeTiers}, #{item.status})" +
            " </foreach>" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int batchInsert(@Param("items") List<ActivityPrizeDo> activityPrizeDOList);

    @Select("select * from activity_prize where activity_id = #{activityId}")
    List<ActivityPrizeDo> selectByActivityId(@Param("activityId") Long activityId);

    /**
     * 根据活动id和奖品id查询活动奖品关联表
     * @param activityId
     * @param prizeId
     * @return
     */
    @Select("select * from activity_prize where activity_id = #{activityId} and prize_id = #{prizeId}")
    ActivityPrizeDo selectByAPId(Long activityId, Long prizeId);

    /**
     * 查询进行中的活动的奖品个数
     * @param activityId
     * @param name
     */
    @Select("select count(1) from activity_prize where activity_id = #{activityId} and status = #{name}")
    int countRunningPrize(Long activityId, String name);

    /**
     * 修改活动关联奖品的状态
     * @param activityId
     * @param prizeId
     * @param name
     */
    @Update("update activity_prize set status = #{name} where activity_id = #{activityId} and prize_id = #{prizeId}")
    void updateStatus(Long activityId, Long prizeId, String name);
}
